<template>
  <div style="padding:30px;">
    <div class="app-container">
      <div class="filter-container">
        <el-input v-model="querySn" placeholder="订单号" style="width: 200px; margin: 5px 0 5px 5px" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-input v-model="queryMobile" placeholder="用户手机号" style="width: 200px; margin: 5px 0 5px 5px" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-select v-model="queryStats" placeholder="状态" clearable style="width: 120px" class="filter-item">
          <el-option v-for="item in statsOptions" :key="item" :label="item" :value="item" />
        </el-select>
        <el-date-picker v-model="settlementDate" style="width: 260px; margin: 5px 0 0 5px" type="datetimerange" :picker-options="settlementPickerOptions" range-separator="至" start-placeholder="结算开始日期" end-placeholder="结算结束日期" />
        <el-date-picker v-model="paymentDate" style="width: 260px; margin: 5px 0 0 5px" type="datetimerange" :picker-options="paymentPickerOptions" range-separator="至" start-placeholder="支付开始日期" end-placeholder="支付结束日期" />
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" />
      </div>
      <!--      列表页包含ID/商品图片/商品名称/商品价格/进货地址/商品订单号/买家收货信息/物流查询/状态/查询/筛选-->
      <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column label="id" align="center">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="订单号" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.order_sn }}
          </template>
        </el-table-column>
        <el-table-column label="付款时间" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.pay_time }}
          </template>
        </el-table-column>
        <el-table-column label="结算时间" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.settlement_time }}
          </template>
        </el-table-column>
        <el-table-column label="付款金额/结算金额" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.payment_amount }}/{{ scope.row.settlement_amount }}
          </template>
        </el-table-column>
        <el-table-column label="商品售价/佣金/佣金比例" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.price }}/{{ scope.row.commission }}/{{ scope.row.commission_ratio }}
          </template>
        </el-table-column>
        <el-table-column label="提成(收入比率)" width="180" align="center">
          <template slot-scope="scope">
            {{ scope.row.income }}({{ scope.row.income_ratio }})
          </template>
        </el-table-column>
        <el-table-column label="技术服务费/费率" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.tech_service_fee }}({{ scope.row.tech_service_rate }})
          </template>
        </el-table-column>
        <el-table-column label="付款/结算预估收入" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.effect_estimates }}/{{ scope.row.estimated_income }}
          </template>
        </el-table-column>
        <el-table-column label="商品标题" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.title }}
          </template>
        </el-table-column>
        <el-table-column label="商品主图" width="120" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.image" min-width="70" height="70">
          </template>
        </el-table-column>
        <el-table-column label="店铺/掌柜旺旺" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.shop_title }}/{{ scope.row.aliwangwang }}
          </template>
        </el-table-column>
        <el-table-column label="晒单会员/手机号" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.member_nickname }}/{{ scope.row.member_mobile }}
          </template>
        </el-table-column>
        <el-table-column label="订单状态" width="120" align="center">
          <template slot-scope="{row}">
            <el-tag :type="row.order_stats | statsCssFilter">
              {{ row.order_stats | statsFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="导入时间" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="currentPage" :limit.sync="pageSize" @pagination="fetchData({})" />

    </div>
  </div>
</template>

<script>
import { getList } from '@/api/tbkOrder'
import Pagination from '@/components/Pagination'

export default {
  components: { Pagination },
  filters: {
    statsFilter(stats) {
      const statsMap = {
        0: '用户晒单',
        1: '订单失败',
        2: '订单付款',
        3: '订单成功',
        4: '订单结算',
        5: '为会员添加余额'
      }
      return statsMap[stats]
    },
    statsCssFilter(stats) {
      const statsMap = {
        0: 'info',
        1: 'danger',
        2: 'info',
        3: 'warn',
        4: 'info',
        5: 'success'
      }
      return statsMap[stats]
    },
  },
  data() {
    return {
      hidden: false,
      listLoading: true,
      // 翻页相关数据 当前页，每页显示条数，排序方式，排序字段，符合条件的搜索结果数，搜索结果
      currentPage: 1,
      pageSize: 10,
      sort: 'asc',
      order: 'created_at',
      total: 0,
      list: [],
      queryMobile: '',
      querySn: '',
      queryStats: '',
      settlementDate: '',
      paymentDate: '',
      date: '',
      sortBy: '',
      // 搜索条件相关数据
      settlementPickerOptions: {
        shortcuts: [{
          text: '昨天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近3天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      paymentPickerOptions: {
        shortcuts: [{
          text: '昨天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近3天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      typeOptions: ['金币转入', '金币消费', '金币转余额'],
      statsOptions: ['用户晒单', '订单失败', '订单付款', '订单成功', '订单结算', '为会员添加余额'],
      showReviewer: false,
      // 弹出层数据
      // 是否隐藏弹出层
      dialogFormVisible: false,
      // 临时数据
      temp: {
        id: undefined
      },
      // 上级分类数据
      parentArr: {},
      rules: {},
      // 下载的loading
      downloadLoading: false,
      downloadData: []
    }
  },
  mounted() {},
  created() {
    this.fetchData()
  },
  methods: {
    handleFilter() {
      this.currentPage = 1
      this.fetchData()
    },
    handleDownload() {
      this.downloadLoading = true
      this.downloadLoading = false
    },
    formatJson(filterVal) {
    },
    fetchData() {
      this.listLoading = true
      const param = {
        sort: this.sort,
        order: this.order,
        page: this.currentPage,
        page_size: this.pageSize
      }
      if (this.querySn !== '') {
        param.sn = this.querySn
      }
      if (this.queryMobile !== '') {
        param.mobile = this.queryMobile
      }
      if (this.queryStats !== '') {
        this.statsOptions.map((item, idx) => {
          if (this.queryStats === item) {
            param.order_stats = idx
          }
        })
      }
      if (this.settlementDate !== '' && this.settlementDate !== null) {
        param.start_settlement_at = this.settlementDate[0].toLocaleDateString() + ' ' + this.settlementDate[0].toTimeString().split(' ')[0]
        param.end_settlement_at = this.settlementDate[1].toLocaleDateString() + ' ' + this.settlementDate[1].toTimeString().split(' ')[0]
      }
      if (this.paymentDate !== '' && this.paymentDate !== null) {
        param.start_payment_at = this.paymentDate[0].toLocaleDateString() + ' ' + this.paymentDate[0].toTimeString().split(' ')[0]
        param.end_payment_at = this.paymentDate[1].toLocaleDateString() + ' ' + this.paymentDate[1].toTimeString().split(' ')[0]
      }
      console.log(param)
      getList(param).then(response => {
        const { data, meta } = response
        this.list = data
        this.listLoading = false
        this.currentPage = parseInt(meta.current_page)
        this.total = parseInt(meta.total)
      }).catch(() => {
        this.listLoading = false
        this.list = []
      })
    }
  }
}
</script>

<style scoped>
  .filter-container {
    background: #fff;
    padding: 32px 16px;
  }
</style>
